package com.wofish.web.controller.goods;

import com.wofish.common.annotation.Log;
import com.wofish.common.core.controller.BaseController;
import com.wofish.common.core.domain.AjaxResult;
import com.wofish.common.core.page.TableDataInfo;
import com.wofish.common.enums.BusinessType;
import com.wofish.common.json.JsonUtil;
import com.wofish.common.utils.poi.ExcelUtil;
import com.wofish.goods.domain.SpecValues;
import com.wofish.goods.domain.Specification;
import com.wofish.goods.service.ISpecValuesService;
import com.wofish.goods.service.ISpecificationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品规格 信息操作处理
 * 
 * @author wofish
 * @date 2019-04-13
 */
@Api("规格管理")
@Controller
@RequestMapping("/goods/specification")
public class SpecificationController extends BaseController
{
    private String prefix = "goods/specification";
	
	@Autowired
	private ISpecificationService specificationService;

	@Autowired
	private ISpecValuesService specValuesService;
	
	@RequiresPermissions("goods:specification:view")
	@GetMapping()
	public String specification()
	{
	    return prefix + "/specification";
	}
	
	/**
	 * 查询商品规格列表
	 */
	@ApiOperation("获取规格列表")
	@RequiresPermissions("goods:specification:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Specification specification)
	{
		startPage();
        List<Specification> list = specificationService.selectSpecificationList(specification);
		return getDataTable(list);
	}

	@PostMapping("/getSpecList")
	@ResponseBody
	public List<Specification> getSpecList(){
		List<Specification> list = specificationService.selectSpecificationList(new Specification());
		if(CollectionUtils.isNotEmpty(list)){//待优化，改为一次查询，而不是n+1次查询
			for (Specification spec : list) {
				SpecValues sv = new SpecValues();
				sv.setSpecId(spec.getSpecId());
				List<SpecValues> specValuesList = specValuesService.selectSpecValuesList(sv);
				if(CollectionUtils.isNotEmpty(specValuesList)){
					spec.setSpecValues(specValuesList.stream().map(bean -> bean.getSpecValue()).collect(Collectors.joining("、")));
				}else {
					spec.setSpecValues(" ");
				}
			}
		}
		return list;
	}
	
	
	/**
	 * 导出商品规格列表
	 */
	@RequiresPermissions("goods:specification:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Specification specification)
    {
    	List<Specification> list = specificationService.selectSpecificationList(specification);
        ExcelUtil<Specification> util = new ExcelUtil<Specification>(Specification.class);
        return util.exportExcel(list, "specification");
    }
	
	/**
	 * 新增商品规格
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存商品规格
	 */
	@ApiOperation("新增保存规格")
	@ApiImplicitParam(name = "specification", value = "新增规格信息", dataType = "Specification")
	@RequiresPermissions("goods:specification:add")
	@Log(title = "商品规格", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Specification specification, String specValuesJson){

		int row = 0;
		if (specification.getSpecId() == null) {
			row = specificationService.insertSpecificationAndValue(specification, specValuesJson);
		}
		return toAjax(row);
	}

	/**
	 * 修改商品规格
	 */
	@GetMapping("/edit/{specId}")
	public String edit(@PathVariable("specId") Long specId, ModelMap mmap)
	{
		Specification specification = specificationService.selectSpecificationById(specId);
		mmap.put("specification", JsonUtil.objToStr(specification));


		/** 查询规格值 */
		SpecValues sv = new SpecValues();
		sv.setSpecId(specId);
		List<SpecValues> specValuesList = specValuesService.selectSpecValuesList(sv);
		mmap.put("specValuesList", JsonUtil.objToStr(specValuesList));

	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存商品规格
	 */
	@RequiresPermissions("goods:specification:edit")
	@Log(title = "商品规格", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Specification specification, String specValuesJson)
	{
		int row = specificationService.updateSpecification(specification, specValuesJson);
		return toAjax(row);
	}
	
	/**
	 * 删除商品规格
	 */
	@RequiresPermissions("goods:specification:remove")
	@Log(title = "商品规格", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(specificationService.deleteSpecificationByIds(ids));
	}
	
}
